#!/usr/bin/python
# -*- coding: utf8 -*-

# 委托任务基础类

import sys
import time

import pyautogui as gui
from termcolor import colored

import auto_gaming_utils as utils


class CommissionBase:

    farm_round = 0
    endless_round = 0 # 单次无尽委托中，已经进行的轮次
    is_endless = False # 是否为无尽委托

    using_double_book = False # 是否使用双倍掉落书
    endless_round_max = 3 # 单次无尽委托中，最多进行的轮次

    def __init__(self, use_double_book=False):
        self.using_double_book = use_double_book

        gui.FAILSAFE = True  # 安全模式：当鼠标移动到屏幕左上角时自动中止程序  默认为True，强烈建议保持启用
        gui.PAUSE = 0.1  # 所有PyAutoGUI函数之间的默认暂停时间（秒）  可调整以模拟不同的操作速度
        gui.FAILSAFE_POINTS = [(0, 0), (1, 0), (0, 1), (1, 1)] # 自动中止的安全区域坐标点 默认值

        # 在特定窗口内执行操作
        if not utils.fetch_game_window():
            print(colored("没有获取到游戏窗口!", "red", attrs=['bold']))
            sys.exit(1)

    def start_farming(self, go_farm_place):

        print("设置窗口大小为：1280 x 720")
        utils.resize_win_to_correct_size()
        print("移动窗口至左下角")
        utils.move_win_to_left_bottom()

        while not utils.GAME_WINDOW.isActive:
            print("等待窗口被激活。")
            time.sleep(2)

        img_commission_start = utils.ImgElement("commission", "委托中队长血条.png",
                                                95, 736, 155, 14)
        img_commission_start.debug = True
        img_choose_after_commission = utils.ImgElement("commission", "委托结束选择.png",
                                                       820, 670, 450, 50)
        img_choose_after_endless = utils.create_img_element_by_points("commission", "无尽委托-轮次结束选择.png",
                                                                      801, 537, 898, 561)

        while utils.GAME_WINDOW.isActive:

            self.farm_round = self.farm_round + 1
            self.endless_round = 1
            img_commission_start.wait_until_match('等待开局', 1)
            print("已开局。", colored(f"当前第 {self.farm_round} 轮。", 'green', attrs=['bold']))
            time.sleep(2)

            go_farm_place(self.farm_round)

            def handle_book_choise():
                if self.using_double_book:
                    print("点击双倍书")
                    utils.click_button_in_win(488, 420, 561, 468)
                    time.sleep(1)

                print("点击“开始挑战”")
                utils.click_button_in_win(547, 528, 737, 540)

            if self.is_endless:
                while True:
                    print(f"当前无尽模式第 {self.endless_round} 轮")
                    img_choose_after_endless.wait_until_match("刷怪中", 3)
                    self.endless_round = self.endless_round + 1
                    if self.endless_round <= self.endless_round_max:
                        # 点击“继续挑战”
                        utils.click_button_in_win(796, 543, 899, 560)
                        time.sleep(0.9)
                        handle_book_choise()
                    else:
                        # 点击“撤离”
                        utils.click_button_in_win(401, 543, 466, 560)
                        img_choose_after_commission.wait_until_match('等待结算界面', 1)
                        break
            else:
                img_choose_after_commission.wait_until_match('刷怪中', 3)

            # 在“委托完成”界面，点击“再次进行”
            print("点击“再次进行”")
            utils.click_button_in_win(838, 680, 1010, 697)
            time.sleep(1.71)

            handle_book_choise()